﻿using Basic.FormModel.DB;
using Basic.FormModel.UserRecord;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class UserFormRecordDAL : BasicDAL<DBUserFormRecord, long>, IUserFormRecordDAL
    {
        public UserFormRecordDAL ( IRepository<DBUserFormRecord> basicDAL ) : base(basicDAL, "user.record")
        {
        }
        public UserRecordDto Add ( UserRecordAdd record )
        {
            DateTime now = DateTime.Now;
            DBUserFormRecord add = new DBUserFormRecord
            {
                EmpId = record.EmpId,
                FormId = record.FormId,
                Id = IdentityHelper.CreateId(),
                LastUpdateTime = now,
                RecordTime = now,
                IsNull = record.Items.IsNull(),
                LinkPK = record.LinkPK,
                LinkType = record.LinkType
            };
            if ( add.IsNull )
            {
                this._BasicDAL.Insert(add);
                return new UserRecordDto
                {
                    Id = add.Id,
                    IsNull = add.IsNull,
                    LastUpdateTime = add.LastUpdateTime
                };
            }
            DBUserFormItem[] items = record.Items.ConvertAll<UserRecordItem, DBUserFormItem>(a => new DBUserFormItem
            {
                ColName = a.ColName,
                ColumnId = a.ColumnId,
                ItemType = a.ItemType,
                StrVal = a.StrVal,
                ColType = a.ColType,
                Id = IdentityHelper.CreateId(),
                IsNull = a.IsNull,
                NumVal = a.NumVal,
                RecordId = add.Id,
                RowIndex = a.RowIndex,
                TableId = a.TableId,
                BoolVal = a.BoolVal,
                LongVal = a.LongVal,
            });
            ISqlQueue<DBUserFormRecord> queue = this._BasicDAL.BeginQueue();
            queue.Insert(add);
            queue.InsertBy<DBUserFormItem>(items);
            _ = queue.Submit();
            return new UserRecordDto
            {
                Id = add.Id,
                IsNull = add.IsNull,
                LastUpdateTime = add.LastUpdateTime,
                Items = items.ConvertAll(a => new FormModel.RecordItem.RecordItemDto
                {
                    RowIndex = a.RowIndex,
                    BoolVal = a.BoolVal,
                    ColName = a.ColName,
                    ColType = a.ColType,
                    IsNull = a.IsNull,
                    StrVal = a.StrVal,
                    ItemType = a.ItemType,
                    LongVal = a.LongVal,
                    NumVal = a.NumVal,
                    TableId = a.TableId
                })
            };
        }
        public UserRecordDto Set ( long id, long empId, UserRecordItem[] Items )
        {
            DateTime now = DateTime.Now;
            ISqlQueue<DBUserFormRecord> queue = this._BasicDAL.BeginQueue();
            queue.Update(new DBUserFormRecord
            {
                Id = id,
                EmpId = empId,
                IsNull = Items.IsNull(),
                LastUpdateTime = now,
            }, new string[]
            {
                "EmpId",
                "IsNull",
                "LastUpdateTime"
            });
            queue.DeleteBy<DBUserFormItem>(a => a.RecordId == id);
            DBUserFormItem[] adds = null;
            if ( !Items.IsNull() )
            {
                adds = Items.ConvertAll(a => new DBUserFormItem
                {
                    ColName = a.ColName,
                    ColumnId = a.ColumnId,
                    StrVal = a.StrVal,
                    Id = IdentityHelper.CreateId(),
                    IsNull = a.IsNull,
                    NumVal = a.NumVal,
                    ColType = a.ColType,
                    RecordId = id,
                    RowIndex = a.RowIndex,
                    TableId = a.TableId,
                    BoolVal = a.BoolVal,
                    ItemType = a.ItemType,
                    LongVal = a.LongVal,
                });
                queue.InsertBy<DBUserFormItem>(adds);
            }
            _ = queue.Submit();
            return new UserRecordDto
            {
                Id = id,
                IsNull = Items.IsNull(),
                LastUpdateTime = now,
                Items = adds.ConvertAll(a => new FormModel.RecordItem.RecordItemDto
                {
                    RowIndex = a.RowIndex,
                    BoolVal = a.BoolVal,
                    ColName = a.ColName,
                    ColType = a.ColType,
                    IsNull = a.IsNull,
                    StrVal = a.StrVal,
                    ItemType = a.ItemType,
                    LongVal = a.LongVal,
                    NumVal = a.NumVal,
                    TableId = a.TableId
                })
            };
        }
    }
}
